﻿using System.Windows;
using System.Windows.Controls;
using System.Linq;

using Tambourine.Model;
using System.Windows.Data;
using System.Collections.Generic;

namespace Tambourine.View
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class TambourineView : Window
    {
        public TambourineView()
        {
            InitializeComponent();
        }

        private void PivotDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.PropertyType == typeof(TestResult))
            {
                CustomBoundColumn newColumn = new CustomBoundColumn()
                {
                    Binding = ((DataGridBoundColumn)e.Column).Binding,
                    TemplateName = "CustomTemplate",
                    Header = e.PropertyName,
                    HeaderStyle = (Style)FindResource("DataGridColumnHeaderAngle"),
                };

                e.Column = newColumn;
            }
        }

        bool detailsExpanderState;
        private void PivotDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            TestResult currentResult = getCurrentResultFromSelectedCells(e.AddedCells);
            detailsExpander.DataContext = currentResult;

            if (currentResult == null || currentResult.IsSuccessful)
            { //hide details 
                if (detailsExpander.IsEnabled)
                {
                    detailsExpanderState = detailsExpander.IsExpanded;
                    if (detailsExpander.IsExpanded)
                        detailsExpander.IsExpanded = false;
                    detailsExpander.IsEnabled = false;
                }
            }
            else
            { //show details
                if (!detailsExpander.IsEnabled)
                {
                    detailsExpander.IsEnabled = true;
                    if (detailsExpanderState)
                        detailsExpander.IsExpanded = true;
                }
            }
        }

        private TestResult getCurrentResultFromSelectedCells(IList<DataGridCellInfo> addedCells)
        {
            try
            {
                DataGridCellInfo firstSelectedCellInfo = addedCells.FirstOrDefault();
                CustomBoundColumn column = (CustomBoundColumn)firstSelectedCellInfo.Column;
                string propName = ((Binding)column.Binding).Path.Path;
                TestCortege curCortege = (TestCortege)firstSelectedCellInfo.Item;
                TestResult curResult = curCortege[propName] as TestResult;
                return curResult;
            }
            catch
            {
                return null;
            }
        }

        private bool wasOnceExpanded = false;
        private void detailsExpander_Expanded(object sender, RoutedEventArgs e)
        {
            if (!wasOnceExpanded)
            {
                wasOnceExpanded = true;
                detailsColumn.Width = new GridLength(400);
            }
        }
    }
}
